-- config ui for autotest

local gui = require("gui")
local widgets = require("gui.widgets")
local autotest = reqscript("autotest")

local REFRESH_MS = 10000

Autotest = defclass(Autotest, widgets.Window)
Autotest.ATTRS{
    frame_title = "Autotest",
    resizable=true,
    autoarrange_subviews=true,
    frame = {w=30, h=11, r=2},
}

function Autotest:init()
    self.data = autotest.data
    self:addviews{
        widgets.ToggleHotkeyLabel{
            view_id="enable_toggle",
            frame={t=0, l=0, w=31},
            label="Autotest is",
            options={{value=true, label="Enabled", pen=COLOR_GREEN},
                     {value=false, label="Disabled", pen=COLOR_RED}},
            on_change=function(val) dfhack.run_command{val and "enable" or "disable", "autotest"} end
        },
    }
    self:refresh_data()
end

function Autotest:refresh_data()
    self.subviews.enable_toggle:setOption(autotest.enabled)
    self.next_refresh_ms = dfhack.getTickCount() + REFRESH_MS
end


---
-- AutotestScreen
---
AutotestScreen = defclass(AutotestScreen, gui.ZScreen)
AutotestScreen.ATTRS{focus_path = "autotestui"}

function AutotestScreen:init()
    self:addviews{Autotest{}}
end

function AutotestScreen:onDismiss()
    view = nil
end
if not dfhack.isMapLoaded then
    qerror("autotest requires a map to be loaded")
end

view = view and view:raise() or AutotestScreen{}:show()
